<?php
class Line_price_model extends CI_Model
{
    private $table = 'line_price';

    public function findById($price_id) {
        
        $this->db->where('id',$price_id);
        return $this->db->get($this->table);
    }
    
    public function findByLineIdDate($line_id)
    {
        $this->db->where('line_id', $line_id);
        $this->db->where('status', 1);
        $this->db->where('line_price.start_date >=',date('Y-m-d', time()).' 00:00:00'); //价格起始于当天
        return $this->db->get($this->table);
    }

    /**
     * 减掉库存操作
     * @param unknown $id
     * @param unknown $line_stock
     */
    public function updateLineStock($id, $line_stock)
    {
        $this->db->set('line_stock', 'line_stock-'.$line_stock, false);
        $this->db->where('id', $id);
        return $this->db->update($this->table);
    }
    
    /**
     * 退回库存操作，增加
     * @param unknown $orderProduct
     * @param unknown $line_stock
     */
    public function returnLineStock($orderProduct,$line_stock)
    {
        $this->db->set('line_stock', 'line_stock+'.$line_stock, false);
        $this->db->where('line_id', $orderProduct->line_id);
        $this->db->where('start_date', $orderProduct->start_date);
        return $this->db->update($this->table);
    }
}